<?php

/**
 * wmfl.cn version 5.0
 * -----------------------------------------------
 * @author esinger <esinger@qq.com>
 * @link http://www.utuo88.com
 * @copyright 2014 Utuo Tech co.,Ltd
 */

/**
 * 地区类
 */
namespace Utuo;

abstract class Region {
	/**
	 * 三级地区数据表名
	 * @var array
	 */
	private static $dbTables = array(
			'province' => 'cn_province',
			'city' => 'cn_city',
			'area' => 'cn_area'
	);
	
	/**
	 * 获取省份
	 * @param integer $provinceCode
	 * @return array
	 */
	public static function getProvince($provinceCode = null) {
		$m = M(self::$dbTables['province']);
		if (empty($provinceCode)) {
			return $m->select();
		} else {
			return $m->where('code=' . intval($provinceCode))->find();
		}
	}
	
	/**
	 * 获取省份名称
	 * @param integer $provinceCode
	 * @return string
	 */
	public static function getProvinceName($provinceCode) {
		return M(self::$dbTables['province'])->where('code=' . intval($provinceCode))->getField('name');
	}
	
	/**
	 * 获取省份列表
	 * @return array 编码与名称的键值对数组
	 */
	public static function getProvinceList() {
		return M(self::$dbTables['province'])->getField('code,name');
	}
	
	/**
	 * 获取城市名称
	 * @param integer $areaCode
	 * @return string
	 */
	public static function getCityName($cityCode) {
		return M(self::$dbTables['city'])->where('code=' . intval($cityCode))->getField('name');
	}
	
	/**
	 * 获取指定省份的所有城市
	 * @param integer $provinceCode
	 * @return array
	 */
	public static function getCityByProvince($provinceCode) {
		return M(self::$dbTables['city'])->where('fcode=' . intval($provinceCode))->select();
	}
	
	/**
	 * 获取指定省份的所有城市列表（编码与名称的键值对数组）
	 * @param integer $provinceCode
	 * @return array
	 */
	public static function getCityListByProvince($provinceCode) {
		return M(self::$dbTables['city'])->where('fcode=' . intval($provinceCode))->getField('code,name');
	}
	
	/**
	 * 获取地区名称
	 * @param integer $areaCode
	 * @return string
	 */
	public static function getAreaName($areaCode) {
		return M(self::$dbTables['area'])->where('code=' . intval($areaCode))->getField('name');
	}
	
	/**
	 * 获取指定城市的所有地区
	 * @param integer $cityCode
	 * @return array
	 */
	public static function getAreaByCity($cityCode) {
		return M(self::$dbTables['area'])->where('fcode=' . intval($cityCode))->select();
	}
	
	/**
	 * 获取指定城市的所有地区列表（编码与名称的键值对数组）
	 * @param integer $cityCode
	 * @return array
	 */
	public static function getAreaListByCity($cityCode) {
		return M(self::$dbTables['area'])->where('fcode=' . intval($cityCode))->getField('code,name');
	}
	
	/**
	 * 地域数组格式
	 */
	public static function getAreaGroup() {
		$areaGroup = array(
			array(
				"title" => "华东",
				"codes" => array("310000", "320000", "330000", "340000", "360000"),
			),
			array(
				"title" => "华北",
				"codes" => array("110000", "120000", "140000", "370000", "130000", "150000"),
			),
			array(
				"title" => "华中",
				"codes" => array("430000", "420000", "410000"),
			),
			array(
				"title" => "华南",
				"codes" => array("440000", "450000", "350000", "460000"),
			),
			array(
				"title" => "东北",
				"codes" => array("210000", "220000", "230000"),
			),
			array(
				"title" => "西北",
				"codes" => array("610000", "650000", "620000", "640000", "630000"),
			),
			array(
				"title" => "西南",
				"codes" => array("500000", "530000", "520000", "540000", "510000"),
			),
			array(
				"title" => "港澳台",
				"codes" => array("810000", "820000", "710000"),
			),
		);
		return $areaGroup;
	}
	
	/**
	 * 获得省市所有数据
	 */
	public static function getAccoData( $level = 3 ) {
		$data = M("CnProvince a")->join("__CN_CITY__ b on a.code=b.fcode", "left")
						->join("__CN_AREA__ c on b.code=c.fcode", "left")
						->field("a.code as pcode,a.name as pname,b.code as ccode,b.name as cname,c.code as acode,c.name as aname")
						->select();
		$acco = array();
		foreach ( $data as $v ) {
			if ( !$acco[$v['pcode']] and $level >= 1) {
				$acco[$v['pcode']] = array('name' => $v['pname']);
			}
			if ( ($acco[$v['pcode']]) and $v['ccode'] and (!$acco[$v['pcode']]['citys'][$v['ccode']]) and $level >= 2) {
				$acco[$v['pcode']]['citys'][$v['ccode']] = array('name' => $v['cname'], 'code' => $v['ccode']);
			}
			if ( ($acco[$v['pcode']]['citys'][$v['ccode']]) and ($v['acode'] and !$acco[$v['pcode']]['citys'][$v['ccode']]['areas'][$v['acode']]) and $level >= 3 ) {
				$acco[$v['pcode']]['citys'][$v['ccode']]['areas'][$v['acode']] = array('name' => $v['aname'], 'code' => $v['acode']);
			}
		}
		return $acco;
	}
}


// End of file Region.class.php
